package com.tongyi.lizhishop.common;

import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.blankj.utilcode.util.ToastUtils;
import com.classic.common.MultipleStatusView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.tongyi.lizhishop.R;
import com.tongyi.lizhishop.constants.Constants;
import com.wuhenzhizao.titlebar.widget.CommonTitleBar;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by meijun on 17-12-9.
 */

public abstract class RecycleViewActivity extends BaseActivity implements OnRefreshListener, OnLoadmoreListener {

    @BindView(R.id.titlebar)
    protected CommonTitleBar titlebar;

    @BindView(R.id.recyclerview)
    protected RecyclerView recyclerview;
    @BindView(R.id.refreshLayout)
    protected SmartRefreshLayout refreshLayout;
    @BindView(R.id.multiple_status_view)
    protected MultipleStatusView multipleStatusView;
    @BindView(R.id.header)
    public FrameLayout header;
    @BindView(R.id.activity_alert_message)
    LinearLayout activityAlertMessage;
    public int currentPageIndex = 0;
    @BindView(R.id.footer)
    protected FrameLayout footer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycleview);
        ButterKnife.bind(this);
        initRefreshView();
        recyclerview.setLayoutManager(new LinearLayoutManager(this));
        recyclerview.setItemAnimator(new DefaultItemAnimator());
    }

    private void initRefreshView() {
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setOnLoadmoreListener(this);
        refreshLayout.setEnableAutoLoadmore(true);
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        currentPageIndex = 0;
        multipleStatusView.showContent();
        loadData();
    }

    public abstract void loadData();

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        loadData();
    }

    /**
     * 计算下一次请求的页码
     *
     * @param array
     */
    public void calculatePageIndexAndControlRefreshLayout(List array) {
        if (currentPageIndex == 0) {
            refreshLayout.finishRefresh();
            refreshLayout.setLoadmoreFinished(false);
            if (array == null) {
                return;
            }
        } else {
            refreshLayout.finishLoadmore();

        }
        if (array.size() == Constants.perPageSize) {
            // may be has more
            currentPageIndex++;
        } else {
            //not more
            refreshLayout.setLoadmoreFinished(true);
        }


    }

    public void onActivitySuccess(RecyclerView.Adapter adapter, List adapterList, List requestValue) {
        if (currentPageIndex == 0) {
            adapterList.clear();
        }
        calculatePageIndexAndControlRefreshLayout(requestValue);
        if (currentPageIndex == 0 && requestValue.size() == 0) {
            //请求的数据为空;
            multipleStatusView.showEmpty();
        } else {
            adapterList.addAll(requestValue);
            adapter.notifyDataSetChanged();
        }
    }


    public void showError(String msg) {

        if (currentPageIndex == 0) {
            multipleStatusView.showNoNetwork();
        } else {
            ToastUtils.showShort(msg);
        }
    }

}
